<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="名称" prop="appName">
        <el-input
            v-model="queryParams.appName"
            placeholder="请输入软件名称搜索"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8" style="display: flex;align-items: center;justify-content: space-between">
      <el-col :span="6" class="tag-group">
        <el-tag
          v-for="(item, index) in tagItems"
          :key="item.label"
          :effect="tagIndex === index ? 'dark' : 'plain'"
          style="margin-right: 10px"
          @click="handleClick(index)">
          {{ item.label }}
        </el-tag>
      </el-col>
      <!-- <el-col :span="2">
        <span style="font-size: 13px;margin-right: 5px;">首页图标</span>
        <el-switch v-model="content.indexIcon" active-color="#00E266" @change="handleContentChange"></el-switch>
      </el-col>
      <el-col :span="3">
        <span style="font-size: 13px;margin-right: 5px;">我的页图标</span>
        <el-switch v-model="content.myPageIcon" active-color="#00E266" @change="handleContentChange"></el-switch>
      </el-col> -->
      <el-col :span="1.5" :offset="15">
        <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
        >添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="default"
            icon="el-icon-delete"
            size="mini"
            @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="80" align="center" sortable />
      <el-table-column label="软件名称" align="center" prop="appName" />
      <el-table-column label="图标" align="center" prop="appIcon">
        <template slot-scope="scope" v-if="scope.row.appIcon">
          <el-image :src="scope.row.appIcon" fit="cover" style="width: 65px;height: 65px;" :preview-src-list="[scope.row.appIcon]"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="软件安装包" align="center" prop="apkUrl" />
      <el-table-column label="软件介绍" align="center" prop="appIntroduction" />
      <el-table-column label="是否显示" align="center" prop="show">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.show" active-color="#00E266" @change="handleSwitchChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="显示位置" align="center" prop="location">
        <template slot-scope="scope">
          <span>{{ getLocationName(scope.row.location) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column label="创建日期" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改会员管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="显示位置：" prop="location">
          <el-radio-group v-model="form.location">
            <el-radio :label="1">最上方</el-radio>
            <el-radio :label="2">中间</el-radio>
            <el-radio :label="3">下方</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="轮播图：" v-if="form.location === 1" prop="bannerPic">
          <image-upload :limit="9" @input="handleImageInput($event, 'bannerPic')" :value="form.bannerPic"></image-upload>
        </el-form-item>
        <el-form-item label="轮播停留秒数：" prop="bannerStaySeconds" v-if="form.location === 1">
          <el-input v-model="form.bannerStaySeconds" placeholder="请输入">
            <template slot="append">s</template>
          </el-input>
        </el-form-item>
        <el-form-item label="软件名称：" prop="appName">
          <el-input v-model="form.appName" placeholder="请输入软件名称" />
        </el-form-item>
        <el-form-item label="图标：" prop="appIcon">
          <el-upload
            class="avatar-uploader"
            :action="uploadAction"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.appIcon" :src="form.appIcon" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="软件介绍：" prop="appIntroduction">
          <el-input type="textarea" v-model="form.appIntroduction" placeholder="请输入软件介绍" />
        </el-form-item>
        <el-form-item label="本地上传/第三方链接">
          <el-switch v-model="form.jump" active-color="#00E266"></el-switch><span>{{ form.jump ? '第三方' : '本地' }}</span>
          <el-input v-model="form.threeUrl" placeholder="请输入第三方链接" v-if="form.jump" />
          <file-upload
            v-else
            :limit="1"
            :fileType="['apk']"
            :fileSize="100"
            :value="form.apkUrl"
            @input="handleFile"
            >
          </file-upload>
        </el-form-item>
        <el-form-item label="软件主图" v-if="!form.jump" prop="appMainPic">
          <image-upload :limit="9" @input="handleImageInput($event, 'appMainPic')" :value="form.appMainPic"></image-upload>
        </el-form-item>
        <el-form-item label="详情图" v-if="!form.jump" prop="appDetailPic">
          <image-upload :limit="9" @input="handleImageInput($event, 'appDetailPic')" :value="form.appDetailPic"></image-upload>
        </el-form-item>
        <el-form-item label="是否显示" prop="show">
          <el-switch v-model="form.show" active-color="#00E266"></el-switch>
        </el-form-item>
        <el-form-item label="排序：" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAppSpreadCenterList, saveAppSpreadCenterInfo, removeAppSpreadCenterInfo, getSpreadCenterSetInfo, saveSpreadCenterSetInfo } from "@/api/shuiyin/shuiyin";

export default {
  name: "ExtendCenterManage",
  data() {
    return {
      // 上传地址
      uploadAction: process.env.VUE_APP_BASE_API + "/common/ossUpload",
      // 位置数据
      tagItems: [
        {
          value: 0,
          label: '全部'
        }, {
          value: 1,
          label: '最上方'
        }, {
          value: 2,
          label: '中间'
        }, {
          value: 3,
          label: '下方'
        }
      ],
      // 位置下标
      tagIndex: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租户管理表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        appName: '',
        location: 0,
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        appName: [
          { required: true, message: "软件名称不能为空", trigger: "blur" }
        ],
      },
      // content: {},
    };
  },
  created() {
    this.getList();
    // this.getSetInfo();
  },
  methods: {
    /** 查询租户管理列表 */
    getList() {
      this.loading = true;
      getAppSpreadCenterList(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // getSetInfo() {
    //   getSpreadCenterSetInfo().then(response => {
    //     if (response.data) {
    //       this.content = response.data;
    //     } else {
    //       this.content = {
    //         indexIcon: false,
    //         myPageIcon: false
    //       }
    //     }
    //   })
    // },
    // tag标签点击事件
    handleClick(index) {
      this.tagIndex = index;
      this.queryParams.location = index;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getLocationName(value) {
      let valueName = '';
      if (value === 1) {
        valueName = '最上方';
      } else if (value === 2) {
        valueName = '中间';
      } else if (value === 3) {
        valueName = '下方';
      } else {
        valueName = '无';
      }
      return valueName;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        appName: null,
        appIcon: '',
        appIntroduction: null,
        appMainPic: '',
        appDetailPic: '',
        apkUrl: '',
        bannerPic: '',
        bannerStaySeconds: null,
        orderNum: null,
        threeUrl: null,
        show: false,
        jump: true
      };
      // this.resetForm("form");
    },
    handleFile(event) {
      this.form.apkUrl = event[0].url;
    },
    handleImageInput(event, name) {
      this.form[name] = event;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // this.form.jump = false;

      const id = row.id || this.ids
      let obj = JSON.parse(JSON.stringify(row));
      obj.jump = row.apkUrl ? false : true;
      this.form = JSON.parse(JSON.stringify(obj));
      console.log(this.form)
      this.open = true;
      this.title = "修改";
    },
    handleSwitchChange(row) {
      let obj = JSON.parse(JSON.stringify(row));
      saveAppSpreadCenterInfo(obj).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传图标大小不能超过 2MB!');
      }
      return isLt2M;
    },
    handleAvatarSuccess(res, file) {
      this.form.appIcon = res.data;
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      // return
      this.$refs["form"].validate(valid => {
        if (valid) {
          let obj = JSON.parse(JSON.stringify(this.form));
          obj.apkUrl = this.form.jump ? '' : this.form.apkUrl;
          if (this.form.id != null) {
            saveAppSpreadCenterInfo(obj).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            saveAppSpreadCenterInfo(obj).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const memberIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除所选数据项？').then(function() {
        return removeAppSpreadCenterInfo(memberIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleContentChange() {
      saveSpreadCenterSetInfo(this.content).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getSetInfo();
      })
    }
  }
};
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 90px;
    height: 90px;
    line-height: 90px;
    text-align: center;
  }
  .avatar {
    width: 90px;
    height: 90px;
    display: block;
  }
</style>
